#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@File    ：config.py
@Author  ：平
@Date    ：2025/9/28 10:07 
"""

from pydantic_settings import BaseSettings
import logging

from sqlalchemy.engine.create import create_engine
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.orm import DeclarativeBase


class Settings(BaseSettings):
    """
    配置类
    """
    # 应用配置
    APPLICATION: str = ""
    APPLICATION_HOST: str = "0.0.0.0"
    APPLICATION_PORT: int = 8080
    APPLICATION_AUTH_EXCLUDED_URL: list[str] = []
    # 常用配置
    CACHE_DIR_PATH: str = "./cache"
    LOG_DIR_PATH: str = "./log"
    # 向量配置
    DOCUMENT_CHUNK_SIZE: int = 512
    DOCUMENT_CHUNK_OVERLAP: int = 128
    EMBEDDING_MODEL: str = ""
    EMBEDDING_MODEL_BASE_URL: str = ""
    EMBEDDING_MODEL_API_KEY: str = ""
    EMBEDDING_MODEL_DIM: int = 1024
    ELASTICSEARCH_URL: str = "http://127.0.0.1:9200"
    ELASTICSEARCH_USERNAME: str = ""
    ELASTICSEARCH_PASSWORD: str = ""
    ELASTICSEARCH_INDEX_NAME: str = ""
    # 模型配置
    MODEL_BASE_URL: str = ""
    MODEL_API_KEY: str = ""
    MODEL: str = ""
    # 服务配置
    BACKEND_SERVICE_URL: str = ""
    # 搜索工具配置
    SERPER_SEARCH_URL: str = ""
    SERPER_WEBPAGE_URL: str = ""
    SERPER_API_KEY: str = ""
    # 数据库配置
    PG_HOST: str = ""
    PG_PORT: int = 5432
    PG_USER: str = "postgres"
    PG_PASSWORD: str = "postgres"
    PG_DATABASE: str = "postgres"
    POOL_MAX_SIZE: int = 20

    class Config:
        env_file = ".env"


settings: Settings = Settings()

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

engine = create_engine(
    f"postgresql://{settings.PG_USER}:{settings.PG_PASSWORD}@{settings.PG_HOST}:{settings.PG_PORT}/{settings.PG_DATABASE}",
    pool_pre_ping=True,  # 心跳保活
    pool_size=settings.POOL_MAX_SIZE
)
Session = sessionmaker(bind=engine)


class Base(DeclarativeBase):
    pass
